package com.taijihuabao.modules.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.commonutils.BusinessIdUtil;
import com.taijihuabao.common.commonutils.PageResult;
import com.taijihuabao.common.redisconfig.UserLoad;
import com.taijihuabao.modules.entity.*;
import com.taijihuabao.modules.mapper.JournalContentMapper;
import com.taijihuabao.modules.mapper.ModelDataMapper;
import com.taijihuabao.modules.mapper.ModelMapper;
import com.taijihuabao.modules.model.journalmodel.dto.*;
import com.taijihuabao.modules.model.basedto.PageDTO;
import com.taijihuabao.modules.model.journalmodel.vo.JournalContentVO;
import com.taijihuabao.modules.model.journalmodel.vo.ModelFieldVO;
import com.taijihuabao.modules.service.JournalContentService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 日志填写信息
 *
 * @author Tu、xiao
 * @date 2022/1/19 18:28
 */
@Service
public class JournalContentServiceImpl implements JournalContentService {

    @Resource
    MongoTemplate mongoTemplate;
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private ModelDataMapper modelDataMapper;
    @Resource
    private JournalContentMapper journalContentMapper;

    @Override
    public PageInfo<JournalContentEntity> selectJournalListByUid(PageDTO pageDTO) {
        //如果mongo为空的话，从mysql查询
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        Long userId = UserLoad.getUser().getId();
        List<JournalContentEntity> journalContentEntities =
                journalContentMapper.selectJournalListByUid(userId);
        return new PageInfo<>(journalContentEntities);
    }

    @Override
    public List<JournalContentEntity> selectJournalListByDataCode(JournalCodeDTO journalCodeDTO) {
        //如果mongo为空的话，从mysql查询
        ModelEntity modelEntity = modelMapper.selectByCode(journalCodeDTO.getCode());
        List<ModelDataEntity> modelDataList = modelDataMapper.selectModelDataByModelCode(modelEntity.getCode());
        List<JournalContentEntity> journalContentList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(modelDataList)) {
            for (ModelDataEntity modelDataEntity : modelDataList) {
                List<JournalContentEntity> journalContentEntityList = journalContentMapper.selectJournalListByDataCode(modelDataEntity.getCode());
                journalContentList.addAll(journalContentEntityList);
            }
        }
        return journalContentList;
    }

    @Override
    public List<JournalContentMongoEntity> selectByUid(PageDTO pageDTO) {
        Query query = Query.query(Criteria.where("user_id").is(UserLoad.getUser().getId()));
        query.with(Sort.by(Sort.Order.desc("create_time")));
        //query.skip((long) pageDTO.getSize() * (pageDTO.getPage() - 1)).limit(pageDTO.getSize());
//        List<JournalContentVO> journalContentVOList = new ArrayList<>();
//        for (JournalContentMongoEntity journalMongoEntity : journalContentMongoList) {
//            JournalContentVO journalContentVO = new JournalContentVO();
//            journalContentVO.setCode(journalMongoEntity.getCode());
//            journalContentVO.setModelCode(journalMongoEntity.getModelCode());
//            //查询模板标题名
//            Query modelTitleQuery = Query.query(Criteria.where("code").is(journalMongoEntity.getModelCode()));
//            JournalModelMongoEntity journalModelMongoEntity = mongoTemplate.findOne(modelTitleQuery, JournalModelMongoEntity.class);
//            journalContentVO.setModelTitle(journalModelMongoEntity.getModelTitle());
//            journalContentVO.setCreatorTime(journalMongoEntity.getCreatorTime());
//            journalContentVO.setUserId(journalMongoEntity.getUserId());
//            journalContentVO.setUserName(journalMongoEntity.getUserName());
//            List<ModelFieldVO> modelFieldVOList = new ArrayList<>();
//            for (ModelFieldsEntity modelField : journalMongoEntity.getFields()) {
//                ModelFieldVO modelFieldVO = new ModelFieldVO();
//                modelFieldVO.setField(modelField.getField());
//                modelFieldVO.setType(modelField.getType());
//                modelFieldVO.setTitle(modelField.getTitle());
//                modelFieldVO.setValue(modelField.getTextMessage());
//                modelFieldVOList.add(modelFieldVO);
//            }
//            journalContentVO.setModelFields(modelFieldVOList);
//            journalContentVOList.add(journalContentVO);
//        }
        List<JournalContentMongoEntity> journalContentMongoEntities = mongoTemplate.find(query, JournalContentMongoEntity.class);
        for(JournalContentMongoEntity journalContentMongoEntity : journalContentMongoEntities){
            Query query1 = Query.query(Criteria.where("code").is(journalContentMongoEntity.getModelCode()));
            JournalModelMongoEntity one = mongoTemplate.findOne(query1, JournalModelMongoEntity.class);
            journalContentMongoEntity.setModelTitle(one.getModelTitle());
        }
        return journalContentMongoEntities;
    }

    @Override
    public JournalContentMongoEntity selectByCode(JournalCodeDTO journalCodeDTO) {
        Query query = Query.query(Criteria.where("code").is(journalCodeDTO.getCode()));
        query.with(Sort.by(Sort.Order.desc("create_time")));
//        List<JournalContentMongoEntity> journalContentMongoList = mongoTemplate.find(query, JournalContentMongoEntity.class);
//        JournalContentVO journalContentVO = new JournalContentVO();
//        for (JournalContentMongoEntity journalMongoEntity : journalContentMongoList) {
//            journalContentVO.setModelCode(journalMongoEntity.getModelCode());
//            journalContentVO.setCreatorTime(journalMongoEntity.getCreatorTime());
//            journalContentVO.setUserId(journalMongoEntity.getUserId());
//            journalContentVO.setUserName(journalMongoEntity.getUserName());
//            List<ModelFieldVO> modelFieldVOList = new ArrayList<>();
//            for (ModelFieldsEntity modelField : journalMongoEntity.getFields()) {
//                ModelFieldVO modelFieldVO = new ModelFieldVO();
//                modelFieldVO.setField(modelField.getField());
//                modelFieldVO.setType(modelField.getType());
//                modelFieldVO.setTitle(modelField.getTitle());
//                modelFieldVO.setValue(modelField.getTextMessage());
//                modelFieldVOList.add(modelFieldVO);
//            }
//            journalContentVO.setModelFields(modelFieldVOList);
//        }
        return mongoTemplate.findOne(query,JournalContentMongoEntity.class);
    }

    @Override
    public void insertJournalContent(JournalCrFromDTO journalCrFromDTO) {
        //mongo模板内容插入
        JournalContentMongoEntity journalContentMongoEntity = new JournalContentMongoEntity();
        journalContentMongoEntity.setCode(BusinessIdUtil.createBusinessId("fillout"));
        journalContentMongoEntity.setModelCode(journalCrFromDTO.getModelCode());
        journalContentMongoEntity.setUserId(UserLoad.getUser().getId());
        journalContentMongoEntity.setUserName(UserLoad.getUser().getName());
        journalContentMongoEntity.setCreatorTime(new Date());
        //根据模板编码查询模板字段
        Query query = Query.query(Criteria.where("code").is(journalCrFromDTO.getModelCode()));
        JournalModelMongoEntity one = mongoTemplate.findOne(query, JournalModelMongoEntity.class);
        List<ModelFieldsEntity> fields = one.getFields();
        //mongo字段集合
        List<ContentFieldsEntity> contentFields = new ArrayList<>();
        for (JournalDTO journalDTO : journalCrFromDTO.getJournalList()) {
            for(ModelFieldsEntity modelFieldsEntity : fields){
                if(journalDTO.getModelDataCode().equals(modelFieldsEntity.getField())){
                    ContentFieldsEntity contentFieldsEntity = new ContentFieldsEntity();
                    contentFieldsEntity.setValue(journalDTO.getTextMessage());
                    contentFieldsEntity.setField(journalDTO.getModelDataCode());
                    contentFieldsEntity.setTitle(modelFieldsEntity.getTitle());
                    contentFieldsEntity.setType(modelFieldsEntity.getType());
                    contentFieldsEntity.setOptions(modelFieldsEntity.getOptions());
                    contentFields.add(contentFieldsEntity);
                }
            }
        }
        journalContentMongoEntity.setFields(contentFields);
        //mongo插入用户填写内容
        mongoTemplate.save(journalContentMongoEntity);
////            JournalContentEntity journalContentEntity = new JournalContentEntity();
////            journalContentEntity.setUserId(journalContentMongoEntity.getUserId());
////            journalContentEntity.setUserName(journalContentMongoEntity.getUserName());
////            journalContentEntity.setCode(BusinessIdUtil.createBusinessId("MDC"));
////            journalContentEntity.setModelDataCode(journalDTO.getModelDataCode());
////            journalContentEntity.setTextMessage(journalDTO.getTextMessage());
////            //mysql插入用户填写内容
////            journalContentMapper.insertJournalContent(journalContentEntity);
////            //模板字段查询
////            ModelDataEntity modelDataEntity = modelDataMapper.selectModelDataByModelDataCode(journalContentEntity.getModelDataCode());
//            //模板字段内容添加到集合
//            ModelFieldsEntity modelFieldsEntity = new ModelFieldsEntity();
//            modelFieldsEntity.setField(journalDTO.getModelDataCode());
//            modelFieldsEntity.setTitle(one.getFields());
//            modelFieldsEntity.setMessageCode(journalDTO.getTextMessage());
//            Query modelFieldQuery = Query.query(Criteria.where("fields.field").is(modelFieldsEntity.getField()));
//            JournalModelMongoEntity modelFieldEntity = mongoTemplate.findOne(modelFieldQuery, JournalModelMongoEntity.class);
//            modelFieldsEntity.setTextMessage(journalContentEntity.getTextMessage());
//            modelFieldsEntityList.add(modelFieldsEntity);
    }

    @Override
    public void updateJournalContent(JournalUpFromDTO journalUpFromDTO) {
        //查询旧单，清楚数据，新增数据
        Query query = Query.query(Criteria.where("code").is(journalUpFromDTO.getCode()));
        JournalContentMongoEntity one = mongoTemplate.findOne(query, JournalContentMongoEntity.class);
        mongoTemplate.remove(query,JournalContentMongoEntity.class);
        JournalContentMongoEntity journalContentMongoEntity = new JournalContentMongoEntity();
        journalContentMongoEntity.setCode(one.getCode());
        journalContentMongoEntity.setModelCode(one.getModelCode());
        journalContentMongoEntity.setUserId(UserLoad.getUser().getId());
        journalContentMongoEntity.setUserName(UserLoad.getUser().getName());
        journalContentMongoEntity.setCreatorTime(new Date());
        //根据模板编码查询模板字段
        Query query2 = Query.query(Criteria.where("code").is(journalContentMongoEntity.getModelCode()));
        JournalModelMongoEntity two = mongoTemplate.findOne(query2, JournalModelMongoEntity.class);
        List<ModelFieldsEntity> fields = two.getFields();
        //mongo字段集合
        List<ContentFieldsEntity> contentFields = new ArrayList<>();
        for (JournalDTO journalDTO : journalUpFromDTO.getJournalList()) {
            for(ModelFieldsEntity modelFieldsEntity : fields){
                if(journalDTO.getModelDataCode().equals(modelFieldsEntity.getField())){
                    ContentFieldsEntity contentFieldsEntity = new ContentFieldsEntity();
                    contentFieldsEntity.setValue(journalDTO.getTextMessage());
                    contentFieldsEntity.setField(journalDTO.getModelDataCode());
                    contentFieldsEntity.setTitle(modelFieldsEntity.getTitle());
                    contentFieldsEntity.setType(modelFieldsEntity.getType());
                    contentFieldsEntity.setOptions(modelFieldsEntity.getOptions());
                    contentFields.add(contentFieldsEntity);
                }
            }
        }
        journalContentMongoEntity.setFields(contentFields);
        //mongo插入用户填写内容
        mongoTemplate.save(journalContentMongoEntity);
//        //mongo字段集合
//        List<ModelFieldsEntity> modelFieldsEntityList = new ArrayList<>();
//        for (JournalDTO journalDTO : journalUpFromDTO.getJournalList()) {
//            JournalContentEntity journalContentEntity = new JournalContentEntity();
//            journalContentEntity.setModelDataCode(journalDTO.getModelDataCode());
//            journalContentEntity.setTextMessage(journalDTO.getTextMessage());
//            journalContentMapper.updateJournalContent(journalContentEntity);
//            //mongo字段
//            ModelFieldsEntity modelFieldsEntity = new ModelFieldsEntity();
//            modelFieldsEntity.setField(journalContentEntity.getModelDataCode());
//            modelFieldsEntity.setMessageCode(journalContentEntity.getCode());
//            modelFieldsEntity.setTextMessage(journalDTO.getTextMessage());
//            modelFieldsEntityList.add(modelFieldsEntity);
//        }
//        Query queryFind = Query.query(Criteria.where("code").is(journalUpFromDTO.getCode()));
//        JournalContentMongoEntity one = mongoTemplate.findOne(queryFind, JournalContentMongoEntity.class);
//        //修改mongo文档
//        Query query = Query.query(Criteria.where("code").is(one.getCode()));
//        Update update = new Update();
//        update.set("fields", modelFieldsEntityList);
//        mongoTemplate.updateFirst(query, update, JournalContentMongoEntity.class);
    }

}
